(function() {
	Path.on("/goods_details/__basename__.html", function(_current_location) {
		var queryString = _current_location.query;
		var goods_id = queryString.get("id");
		coAjax.get(appConfig.user.can_eval + goods_id, function(result) {
			App.set("$Cache.eval.can_evaluation_able", result.result.can_evaluation_able);
		});
		
		// 这部分是重复的，要改
		/*
		 * 推荐者信息
		 */
		var recommender_id = queryString.get("rcid");
		if (recommender_id) {
			//将推荐者的信息保存起来，或者直接覆盖上一个推荐者，以最后一个推荐者为准
			LS.set("recommender_id", recommender_id);
		}
		App.set("$Cache.recommender_id", recommender_id);
		/*
		 * 二维码链接
		 */
		//定义默认链接
		App.set("$Cache.recommender_url", "http://" + location.host + location.pathname + "?id=" + queryString.get("id"))
		eventManager.is(App.get("loginer._id"), "getLoginer", function() {
			var user_id = App.get("loginer._id");
			App.set("$Cache.recommender_url", "http://" + location.host + location.pathname + "?id=" + queryString.get("id") + "&rcid=" + user_id)
		});
		App.set("goods_id", goods_id);
		//参数错误，返回首页
		if (!goods_id) {
			Path.jump("/main.html");
		};
		coAjax.get(appConfig.goods.detail + goods_id, function(result) {
			jSouper.ready(function() {
				var goods_info = result.result;
				//处理数据格式
				goods_info.price = +goods_info.price || 0;
				goods_info.price_toFixed_2 = goods_info.price.toFixed(2);
				goods_info.fare = +goods_info.fare || 0;
				goods_info.fare_toFixed_2 = goods_info.fare.toFixed(2);
				App.set("goods_detail", goods_info);
			});
		});
	});
	App.set("$Event.eval.submitEval", function() {
		var evaluationData = App.get("$Cache.eval");
		if (!$.trim(evaluationData.content)) {
			alert("warn", "请输入评价内容");
			return;
		};
		coAjax.post(appConfig.user.add_eval + goods_id, evaluationData, function(result) {
			evaluations = App.get("goods_detail.evaluations");
			evaluations.unshift(result.result);
			App.set("goods_detail.evaluations", evaluations);
			App.set("$Cache.eval.can_evaluation_able", false);
		});
	});


	// 这部分是重复的，要改
	/*
	 * 购物车信息
	 */

	App.set("$Event.goods_details.goods.add_to_cart", function(e) {
		App.set("$Cache.top.private", 'focus');
		var cart_item = {
			goods_id: App.get("goods_detail._id"),
			num: App.get("$Cache.goods_details.buy_number")
		};
		if (App.get("loginer")) {
			coAjax.post(appConfig.user.cart, cart_item, _get_cart_success, _get_cart_error);
		} else {
			var cache_cart = LS.get("$Cache.cart");
			try {
				cache_cart = JSON.parse(cache_cart);
			} catch (e) {
				console.error(e)
			}
			if (!(cache_cart instanceof Array)) {
				cache_cart = [];
			}
			cache_cart.push(cart_item)
		}
	});
	/*
	 * 检验是否收藏了这个商品
	 */
	eventManager.is(App.get("loginer.collect"), "getLoginer", function() {
		var collect = App.get("loginer.collect") || [];
		_is_collect_goods(collect);
	});

	function _is_collect_goods(collect) {
		var result = false;
		if (jSouper.indexOf(collect, App.get("goods_id")) !== -1) {
			result = true;
		}
		App.set("$Cache.goods_detail.is_collect", result);
	}
	App.set("$Event.goods_details.goods.collect_toggle", function() {
		if (App.get("$Cache.goods_detail.is_collect")) {
			coAjax["delete"](appConfig.user.collect_remove, {
				goods_id: App.get("goods_id")
			}, function(result) {
				alert("已经取消收藏");
				_is_collect_goods(result.result);
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg)
			});
		} else {
			coAjax.post(appConfig.user.collect_add, {
				goods_id: App.get("goods_id")
			}, function(result) {
				alert("success", "收藏成功");
				_is_collect_goods(result.result);
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg)
			});
		};
	});

}());